package com.ruoyi.web.controller.modules;

import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.domain.park.Park;
import com.ruoyi.web.pojo.dto.park.ParkVo;
import com.ruoyi.web.pojo.request.park.ParkAddReq;
import com.ruoyi.web.pojo.request.park.ParkPageReq;
import com.ruoyi.web.service.park.IParkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;


/**
 * @author zq
 */
@Api(value = "园区信息", tags = "园区信息")
@RestController
@RequestMapping("/park")
public class ParkController extends BaseController {

    private final IParkService parkService;

    public ParkController(IParkService parkService) {
        this.parkService = parkService;
    }

    /**
     * 查询所有记录
     *zq
     * @return 返回集合，没有返回空List
     */
    @ApiOperation("分页查询")
    @PostMapping("/page")
    public TableDataInfo<ParkVo> getPage(@RequestBody ParkPageReq req) {
        startPage();
        List<ParkVo> list = parkService.listAll(Park.toParkPage(req));
        return getDataTable(list);
    }


    /**
     * 根据主键查询
     *zq
     * @param id 主键
     * @return 返回记录，没有返回null
     */
    @ApiOperation("根据主键查询")
    @GetMapping("getById")
    public AjaxResult getById(Long id) {
        return AjaxResult.success(parkService.getById(id));
    }

    /**
     * zq
     * @return 返回记录，没有返回null
     */
    @ApiOperation("获取用户得园区")
    @GetMapping("getByUserId")
    public AjaxResult getByUserId() {
        return AjaxResult.success(parkService.getByUserId(SecurityUtils.getUserId()));
    }
     
    /**
     * 新增，忽略null字段
     *zq
     * @param park 新增的记录
     * @return 返回影响行数
     */
    @ApiOperation("新增")
    @PostMapping("insert")
    @RateLimiter(time = 1,count = 1)
    public AjaxResult insert(@RequestBody @Valid ParkAddReq park) {
        return toAjax(parkService.insertIgnoreNull(Park.toPark(park)));
    }    
      
    /**
     * 修改，忽略null字段
     *zq
     * @param park 修改的记录
     * @return 返回影响行数
     */
    @ApiOperation("修改")
    @PutMapping("update/{id}")
    @RateLimiter(time = 1,count = 1)
    public AjaxResult update(@PathVariable Long id,@RequestBody @Valid ParkAddReq park) {
        Park update = Park.toPark(park);
        update.setId(id);
        update.setUpdateTime(new Date());
        return toAjax(parkService.updateIgnoreNull(update));
    }
    
    /**
     * 删除记录
     *zq
     * @param id 待删除的记录
     * @return 返回影响行数
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        return toAjax(parkService.delete(id));
    }
    
}